package com.ssyc.student.util;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.ssyc.common.base.SpKeys;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.bean.StateInfo;
import com.ssyc.student.bean.TimeRecordInfo;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 记录上传初次进入模块时间和退出模块的时间的网络请求接口
 * Created by Administrator on 2018/6/11 0011.
 */

public class RecordTimeUtil {

    public static String energy;
    /**
     * 进入模块请求服务器记录时间或者亮屏
     *
     * @param context
     * @param listener
     */
    public static void httpEnterTime(Context context,String lessonId,String gradeId,String moduleId, onRecordRowIdListener listener) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "16");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", AccountUtils.getRole(context));
        map.put("gradeId", gradeId);
        map.put("lessonId", lessonId);
        map.put("moduleId", moduleId);
        map.put("apptoken", AccountUtils.getToken(context));
        map.put("platform", "8");
        Log.i("test","gradeId:"+gradeId+"lessonId:"+lessonId+"moduleId:"+moduleId);

        HttpUtils.post(HttpAdress.GRAMMAR, map, context, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast("网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                TimeRecordInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, TimeRecordInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }
                Log.i("test","开始计时："+ response);
                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //返回成功
                        String rowId = info.getData().getROW_ID() + "";
                        if(info.getData().getENERGY()!=0){
                            energy = info.getData().getENERGY() + "";
                        }

                        if(TextUtils.isEmpty(energy)){
                            energy = "1";
                        }
                        if (listener != null) {
                            listener.recordRowId(rowId,energy);

                        }

                    } else {
                        Log.i("test", info.getState());
                    }
                }
            }
        });

    }

    /**
     * 清除Energy
     */
    public static void cleanEnergy() {
        energy = "";
    }


    /**
     * 退出模块或者熄屏请求服务器记录时间
     *
     * @param rowId
     * @param context
     */
    public static void httpExitTime(String rowId, Context context) {

        if (TextUtils.isEmpty(rowId)) {
            return;
        }

        //提交结束时间
        Map<String, String> map = new HashMap<>();
        map.put("type", "17");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", "0");
        map.put("rowid", rowId);
        map.put("platform", "8");
        map.put("apptoken", AccountUtils.getToken(context));

        HttpUtils.post(HttpAdress.GRAMMAR, map, context, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast("网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                StateInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, StateInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }

                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //返回成功
                        Log.i("test", "结束时间提交成功");
                    } else {
                        Log.i("test", info.getState());
                    }
                }
            }
        });
    }


    /**
     * 回调RowId的接口
     */
    public interface onRecordRowIdListener {
        void recordRowId(String recordId,String enetgy);
    }


    /**
     * 进入APP请求服务器记录时间
     *
     * @param context
     */
    public static void httpEnterAppTime(Context context) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "14");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", AccountUtils.getRole(context));
        map.put("os", "android");
        map.put("apptoken", AccountUtils.getToken(context));
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.GRAMMAR, map, context, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast("网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                TimeRecordInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, TimeRecordInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }
                Log.i("test", response);
                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //返回成功
                        String rowId = info.getData().getROW_ID() + "";


                        //保存APP计时参数
                        SPUtil.put(context, SpKeys.TIMERECORD, rowId);

                    } else {
                        Log.i("test", info.getState());
                    }
                }
            }
        });
    }

    /**
     * 退出APP请求服务器记录时间
     *
     * @param rowId
     * @param context
     */
    public static void httpExitAppTime(String rowId, Context context) {

        if (TextUtils.isEmpty(rowId)) {
            return;
        }

        //提交结束时间
        Map<String, String> map = new HashMap<>();
        map.put("type", "15");
        map.put("acc", AccountUtils.getAccount(context));
        map.put("role", "0");
        map.put("rowid", rowId);
        map.put("platform", "8");
        map.put("apptoken", AccountUtils.getToken(context));

        HttpUtils.post(HttpAdress.GRAMMAR, map, context, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast("网络错误", false);
            }

            @Override
            public void onResponse(String response, int id) {
                StateInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, StateInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }
                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //返回成功
                        Log.i("test", "结束时间提交成功");
                    } else {
                        Log.i("test", info.getState());
                    }
                }
            }
        });
    }
}
